package com.example.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.travel.common.AjaxResult;
import com.example.travel.common.AuthAccess;
import com.example.travel.common.TableDataInfo;
import com.example.travel.domain.entity.Role;
import com.example.travel.domain.request.RoleBindMenuRequest;
import com.example.travel.domain.request.PageQuery;
import com.example.travel.service.RoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author : LinQingLiang
 * @file : RoleController.java
 * @Since : 2023/1/30 21:23
 */

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController extends BaseController{

    @Autowired
    private RoleService roleService;

    //分页查询
    @GetMapping("list")
    public TableDataInfo list(@Validated PageQuery pageQuery, Role role){
//        User currentUser = TokenUtils.getUser();
        Page page1 = roleService.page(role, pageQuery.getPageNum(),pageQuery.getPageSize());
        return getDataTable(page1.getRecords(),page1.getTotal());
    }

    //添加
    @PostMapping("add")
    public AjaxResult create(@RequestBody Role role){
        return AjaxResult.success(roleService.save(role));
    }

    //修改前查询
    @GetMapping("{roleId}")
    public AjaxResult selectById(@PathVariable Integer roleId){
        return AjaxResult.success(roleService.getById(roleId));
    }

    @PutMapping("updateRole")
    public AjaxResult updateUser(@RequestBody Role role){
        QueryWrapper<Role> wrapper = new QueryWrapper();
        wrapper.eq("name",role.getName());
        Role role1 = roleService.getOne(wrapper);
        if (role1 != null && !(role1.getName()).equals(role.getName())){
            return AjaxResult.error("用户名已经存在不能进行修改");
        }
        return toAjax(roleService.updateById(role));
    }

    //删除个人
    @DeleteMapping("deleteRole/{roleId}")
    public AjaxResult deleteUser(@PathVariable Integer roleId){
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Role::getId,roleId);
        return AjaxResult.success(roleService.remove(lambdaQueryWrapper));
    }

    //批量删除
    @DeleteMapping("deleteRoles/{roleIds}")
    public AjaxResult deleteUser(@PathVariable List<Integer> roleIds){
        return AjaxResult.success(roleService.removeBatchByIds(roleIds));
    }


    /**
     * 一个角色绑定多个菜单
     * @param roleBindMenuRequest
     * @return
     */
    @PostMapping("roleMenu")
    public AjaxResult roleBindMenu(@RequestBody RoleBindMenuRequest roleBindMenuRequest){
        roleService.roleBindMenu(roleBindMenuRequest.getRoleId(),roleBindMenuRequest.getMenuIds());
        return AjaxResult.success();
    }

    @GetMapping("roleMenu/{roleId}")
    public AjaxResult getRoleBindMenu(@PathVariable Integer roleId){
        return AjaxResult.success(roleService.selectByRoleId(roleId));
    }

    @AuthAccess
    @GetMapping("/rolesName")
    public AjaxResult getRoleName(){
        return AjaxResult.success(roleService.list());
    }

}
